package UILayer;
import ModelLayer.*;
import CtrlLayer.*;
import java.util.*;

public class SalesUI
{
    SaleController saleController;
    public SalesUI(){
        saleController = new SaleController();
    }

    public void salesMenuStart(int sessionId){
        boolean exit=false;
        while (!exit) 
        {
            int choice = writeSalesMenu(sessionId);

            switch(choice){
                case 1:
                startCreateSale(sessionId);
                break;
                case 2:
                startFindSale();
                break;
                case 3:
                startDeleteSale();
                break;
                case 0:
                exit = true;
                break;

            }

        }
    }

    private int writeSalesMenu(int sessionId){ 
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Sales Menu ***");
        System.out.println(" (1) Make a sale");
        System.out.println(" (2) Show a sale");
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println(" (3) Delete a sale"); 
        }
        System.out.println(" (0) Back");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void startCreateSale(int sessionId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("###Sale form:###");
        System.out.println("Enter the ID of the customer;");
        int customerID = Integer.parseInt(keyboard.nextLine());
        int saleId = saleController.createSale(customerID,sessionId);  
        if(saleId != 0) {
            boolean exit = false;
            while(!exit)
            {
               System.out.println(" (1) Add a product");
               System.out.println(" (2) Remove a product");
               System.out.println(" (0) end");
               int choice = Integer.parseInt(keyboard.nextLine());

                switch(choice){
                case 1:
                addProduct(saleId);
                break;
                case 2:
                removeProduct(saleId);
                break;
                case 0:
                exit = true;
                break;
               }
            }

            if(saleController.endSale(saleId, sessionId)) {
                System.out.println("Sale with ID: " + saleId + " has been finished!");
            } else {
                System.out.println("Sale can not be finished! The amount is 0!");
            }
        } else {
            System.out.println("Invalid Customer!");
            saleController.deleteSale(saleId);
        }
        pause();
    }
    
    private void removeProduct(int saleId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Product barcode:");
        int productBarcode = Integer.parseInt(keyboard.nextLine());
        System.out.println("Product amount:");
        int amount = Integer.parseInt(keyboard.nextLine());
        saleController.removeProductFromSale(saleId, productBarcode, amount);
    }

    private void addProduct(int saleId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Product barcode:");
        int productBarcode = Integer.parseInt(keyboard.nextLine());
        System.out.println("Product amount:");
        int amount = Integer.parseInt(keyboard.nextLine());
        boolean check = saleController.addProductInSale(saleId,productBarcode,amount);
        if(!check) {
           System.out.println("There is no such product!");
           saleController.removeProductFromSale(saleId,productBarcode,amount);
        }
    }

     void startFindSale(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("ID");
        int id = Integer.parseInt(keyboard.nextLine());

        if(saleController.findSale(id) != null){
            System.out.println();
            System.out.println("ID of sale: " + saleController.findSale(id).getId());
            System.out.println("Date of sale: " + saleController.findSale(id).getDate());
            System.out.println("-----------Products---------------" );

            for (int key : saleController.findSale(id).getProducts().keySet()) {
                System.out.println("Product: " + key + "\t \t Amount: " + saleController.findSale(id).getProducts().get(key));
            }
            System.out.println("-----------------------------------" );
            System.out.println("\t \t \t Total: " + saleController.findSale(id).getTotalPrice());
            System.out.println("-----------------------------------" );
            System.out.println("Employee: " + saleController.findSale(id).getEmployeeID());
            System.out.println("Customer: " + saleController.findSale(id).getCustomerID());
        } else {
            System.out.println("The sale has not been found!");
        }
        pause();
    }

    private void startDeleteSale(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("ID");
        int id = Integer.parseInt(keyboard.nextLine());
        saleController.deleteSale(id);
    }

    private void pause(){
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}
